package com.whut.whw.chat;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;

import com.whut.whw.R;
import com.whut.whw.base.BaseFragment;
import com.whut.whw.opersoncenter.OPersonCenterActivity;

import io.rong.imkit.RongIM;
import io.rong.imkit.fragment.ConversationListFragment;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.imlib.model.UserInfo;

/**
 * <pre>
 *  @Decription 消息聊天列表
 *  @datetime 2017/06/12 19:03
 *  @author 忘尘无憾
 *  @version
 * </pre>
 */
public class ChatListFragment extends BaseFragment {

    public static ChatListFragment newInstance() {
        return new ChatListFragment();
    }

    Toolbar toolbar;

    @Override
    public View initView(LayoutInflater inflater) {
        View rootView = inflater.inflate(R.layout.fragment_conversation_list, null);
//        toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        return rootView;
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        setToolBar();//设置toolbar

        addRong();//将融云会话列表添加到页面
    }

    /**
     * 设置toolbar
     */
    private void setToolBar() {
//        toolbar.setTitle(getResources().getString(R.string.msg_title).toString());
//        toolbar.setTitleTextColor(ContextCompat.getColor(context, R.color.white));
//        toolbar.inflateMenu(R.menu.fragment_chat_list_nav_menu);
    }

    /**
     * 将融云会话列表添加到页面
     */
    private void addRong() {
        ConversationListFragment listFragment = (ConversationListFragment) ConversationListFragment.instantiate(getActivity(), ConversationListFragment.class.getName());
        Uri uri = Uri.parse("rong://" + getActivity().getApplicationInfo().packageName).buildUpon()
                .appendPath("conversationlist")
                .appendQueryParameter(Conversation.ConversationType.PRIVATE.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.GROUP.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.DISCUSSION.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.PUBLIC_SERVICE.getName(), "false")
                .appendQueryParameter(Conversation.ConversationType.SYSTEM.getName(), "false")
                .build();
        listFragment.setUri(uri);
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        //将融云的Fragment界面加入到我们的页面。
        transaction.add(R.id.conversationlist, listFragment);
        transaction.commitAllowingStateLoss();
    }

    @Override
    public void setListener() {
        RongIM.setConversationBehaviorListener(new RongIM.ConversationBehaviorListener() {
            @Override
            public boolean onUserPortraitClick(Context context, Conversation.ConversationType conversationType, UserInfo userInfo) {
                startActivity(new Intent(context, OPersonCenterActivity.class));
                return false;
            }

            @Override
            public boolean onUserPortraitLongClick(Context context, Conversation.ConversationType conversationType, UserInfo userInfo) {
                return false;
            }

            @Override
            public boolean onMessageClick(Context context, View view, Message message) {
                return false;
            }

            @Override
            public boolean onMessageLinkClick(Context context, String s) {
                return false;
            }

            @Override
            public boolean onMessageLongClick(Context context, View view, Message message) {
                return false;
            }
        });
    }
}
